package com.blue.controller.author;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.entity.ResponseResult;
import com.blue.common.utils.ConvertUtils;
import com.blue.common.utils.QueryWrapperUtils;
import com.blue.dto.author.RoleMenuDTO;
import com.blue.po.author.RoleMenuPO;
import com.blue.service.author.IRoleMenuService;
import com.github.yulichang.query.MPJQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色菜单关联表
 *
 * @author cnj
 * @email 15696471158@163.com
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/author/roleMenu")
@Tag(name = "角色菜单关联表")
public class RoleMenuController {
    @Autowired
    private IRoleMenuService roleMenuPOService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    public ResponseResult<?> list(RoleMenuDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        RoleMenuPO RoleMenuPO = ConvertUtils.convert(RoleMenuPO.class, requestDTO);
        MPJQueryWrapper<RoleMenuPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(RoleMenuPO);
        Page<RoleMenuPO> page = new Page<>(pageNum, pageSize);
        Page<RoleMenuPO> pageResult = roleMenuPOService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody RoleMenuDTO requestDTO) {
        RoleMenuPO RoleMenuPO = ConvertUtils.convert(RoleMenuPO.class, requestDTO);
        roleMenuPOService.save(RoleMenuPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody RoleMenuDTO requestDTO) {
        RoleMenuPO RoleMenuPO = ConvertUtils.convert(RoleMenuPO.class, requestDTO);
        roleMenuPOService.updateById(RoleMenuPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        roleMenuPOService.removeByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody RoleMenuDTO requestDTO) {
        RoleMenuPO RoleMenuPO = roleMenuPOService.getById(requestDTO.getId());
        return ResponseResult.okResult(RoleMenuPO);
    }
}
